package org.apache.ibatis.demo;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;

/**
 * TODO
 *
 * @author yincb
 * @version 1.0
 * @date 2021/9/15
 */
public class MyDemo {

    Logger logger = LoggerFactory.getLogger(MyDemo.class);

  @Test
  public void test() throws IOException {
    String resource = "org/apache/ibatis/demo/mybatis-config.xml";
    InputStream inputStream = Resources.getResourceAsStream(resource);
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

    try (SqlSession session = sqlSessionFactory.openSession()) {
      HashMap<String, Object> map = new HashMap<>();
      map.put("id", 101);
      map.put("name", "zhangsan");
      Student student = (Student) session.selectOne("org.apache.ibatis.demo.StudentMapper.selectStudent2", map);
      //List<Object> objects = session.selectList("org.apache.ibatis.demo.StudentMapper.selectStudent2", map);
      System.out.println(student);
    }

    //  代理模式
    /*try (SqlSession session = sqlSessionFactory.openSession()) {
      StudentMapper mapper = session.getMapper(StudentMapper.class);
      Student student = mapper.selectStudent(101);
      System.out.println(student);
    }*/

  }


}
